﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using HRC.Library.EntityLibrary.EntityBase;
using HRC.Library.EntityLibrary.EntityOperations;

namespace Lehnet.BusinessLayer
{
    internal class EntitySessionManager
    {
        //singleton
        static Lazy<EntitySessionManager> _instance =
            new Lazy<EntitySessionManager>(() => { return new EntitySessionManager(); }, true);
        public static EntitySessionManager Instance
        {
            get
            {
                return _instance.Value;
            }
        }

        internal T GetOldEntity<T>(BaseEntity newEntity) where T : new()
        {
            T oldEntity = EntityManager.Load<T>(" where Id = " + newEntity.GetValue<int>("Id"));

            foreach (PropertyInfo pi in typeof(T).GetProperties())
            {
                pi.SetValue(oldEntity, pi.GetValue(newEntity, null), null);
            }

            return oldEntity;
        }
    }
}
